package org.aceor.mddal.gateway.executor;

import com.google.common.collect.Lists;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 多线程执行预编译语句对象请求的执行器.
 * 
 */
public final class PreparedStatementExecutor {
    
    private final ExecutorEngine executorEngine;
    
    private final Collection<PreparedStatement> preparedStatements;

    public PreparedStatementExecutor() {
        this.executorEngine = ExecutorEngineFactory.getInstance();
        preparedStatements = Lists.newLinkedList();
    }

    /**
     * 执行SQL查询.
     * 
     * @return 结果集列表
     * @throws SQLException SQL异常
     */
    public List<ResultSet> executeQuery() throws SQLException {
        List<ResultSet> result;
        if (1 == preparedStatements.size()) {
            result =  Arrays.asList(preparedStatements.iterator().next().executeQuery());
            return result;
        }
        result = executorEngine.execute(preparedStatements, new ExecuteUnit<PreparedStatement, ResultSet>() {
            
            @Override
            public ResultSet execute(final PreparedStatement input) throws Exception {
                return input.executeQuery();
            }
        });
        return result;
    }
    
    /**
     * 执行SQL更新.
     * 
     * @return 更新数量
     * @throws SQLException SQL异常
     */
    public int executeUpdate() throws SQLException {
        int result;
        if (1 == preparedStatements.size()) {
            result =  preparedStatements.iterator().next().executeUpdate();
            return result;
        }
        result = executorEngine.execute(preparedStatements, new ExecuteUnit<PreparedStatement, Integer>() {
            
            @Override
            public Integer execute(final PreparedStatement input) throws Exception {
                return input.executeUpdate();
            }
        }, new MergeUnit<Integer, Integer>() {
            
            @Override
            public Integer merge(final List<Integer> results) {
                int result = 0;
                for (Integer each : results) {
                    result += each;
                }
                return result;
            }
        });
        return result;
    }
    
    /**
     * 执行SQL请求.
     * 
     * @return true表示执行DQL, false表示执行的DML
     * @throws SQLException SQL异常
     */
    public boolean execute() throws SQLException {
        if (1 == preparedStatements.size()) {
            boolean result = preparedStatements.iterator().next().execute();
            return result;
        }
        List<Boolean> result = executorEngine.execute(preparedStatements, new ExecuteUnit<PreparedStatement, Boolean>() {
            
            @Override
            public Boolean execute(final PreparedStatement input) throws Exception {
                return input.execute();
            }
        });
        return result.get(0);
    }

    public void addPrepareStatement(PreparedStatement preparedStatement) {
        preparedStatements.add(preparedStatement);
    }
}
